// 游戏详情页面的交互逻辑
document.addEventListener('DOMContentLoaded', () => {
    // 初始化变量
    const playButton = document.querySelector('.play-button');
    const fullscreenMode = document.querySelector('.fullscreen-mode');
    const exitFullscreenButton = document.querySelector('.exit-fullscreen');
    const gameContainer = document.getElementById('game-container');
    const screenshots = document.querySelectorAll('.screenshot');
    const screenshotViewer = document.querySelector('.screenshot-viewer');
    const viewerImage = document.querySelector('.viewer-image');
    const viewerClose = document.querySelector('.viewer-close');
    const favoriteButton = document.querySelector('.action-button');

    // 游戏状态
    let isFullscreen = false;
    let isFavorited = false;

    // 全屏模式处理
    playButton.addEventListener('click', () => {
        fullscreenMode.classList.add('active');
        document.body.style.overflow = 'hidden';
        isFullscreen = true;

        // 在这里初始化游戏
        initGame();
    });

    exitFullscreenButton.addEventListener('click', () => {
        fullscreenMode.classList.remove('active');
        document.body.style.overflow = '';
        isFullscreen = false;

        // 在这里暂停或清理游戏
        cleanupGame();
    });

    // ESC键退出全屏
    document.addEventListener('keydown', (e) => {
        if (e.key === 'Escape' && isFullscreen) {
            exitFullscreenButton.click();
        }
    });

    // 截图查看器
    screenshots.forEach(screenshot => {
        screenshot.addEventListener('click', () => {
            const imgSrc = screenshot.querySelector('img').src;
            viewerImage.src = imgSrc;
            screenshotViewer.classList.add('active');
            document.body.style.overflow = 'hidden';
        });
    });

    viewerClose.addEventListener('click', closeViewer);
    screenshotViewer.addEventListener('click', (e) => {
        if (e.target === screenshotViewer) {
            closeViewer();
        }
    });

    function closeViewer() {
        screenshotViewer.classList.remove('active');
        document.body.style.overflow = '';
    }

    // 收藏功能
    favoriteButton.addEventListener('click', () => {
        isFavorited = !isFavorited;
        favoriteButton.style.backgroundColor = isFavorited ? 'var(--primary-color)' : 'transparent';
        favoriteButton.style.color = isFavorited ? 'white' : 'var(--primary-color)';

        // 这里可以添加收藏相关的API调用
        if (isFavorited) {
            showToast('已添加到收藏夹');
        } else {
            showToast('已从收藏夹移除');
        }
    });

    // 消息提示
    function showToast(message) {
        const toast = document.createElement('div');
        toast.className = 'toast';
        toast.textContent = message;
        document.body.appendChild(toast);

        // 添加样式
        toast.style.position = 'fixed';
        toast.style.bottom = '20px';
        toast.style.left = '50%';
        toast.style.transform = 'translateX(-50%)';
        toast.style.backgroundColor = 'rgba(108, 92, 231, 0.9)';
        toast.style.color = 'white';
        toast.style.padding = '12px 24px';
        toast.style.borderRadius = 'var(--radius-sm)';
        toast.style.zIndex = '2000';
        toast.style.opacity = '0';
        toast.style.transition = 'opacity 0.3s ease';

        // 显示动画
        requestAnimationFrame(() => {
            toast.style.opacity = '1';
        });

        // 3秒后消失
        setTimeout(() => {
            toast.style.opacity = '0';
            setTimeout(() => {
                document.body.removeChild(toast);
            }, 300);
        }, 3000);
    }

    // 游戏相关函数
    function initGame() {
        // 这里初始化游戏
        // 例如：创建游戏画布，加载资源等
        console.log('游戏初始化');
    }

    function cleanupGame() {
        // 这里清理游戏资源
        // 例如：停止游戏循环，清除事件监听器等
        console.log('游戏清理');
    }

    // 动态评分星星
    const stars = document.querySelectorAll('.rating-stars .star');
    stars.forEach((star, index) => {
        star.addEventListener('mouseover', () => {
            stars.forEach((s, i) => {
                s.style.opacity = i <= index ? '1' : '0.3';
            });
        });

        star.addEventListener('mouseleave', () => {
            stars.forEach(s => {
                s.style.opacity = '1';
            });
        });

        star.addEventListener('click', () => {
            const rating = index + 1;
            // 这里可以添加评分API调用
            showToast(`感谢您的${rating}星评价！`);
        });
    });

    // 页面加载完成后的动画
    function initPageAnimations() {
        const elements = [
            document.querySelector('.game-header'),
            document.querySelector('.game-description'),
            document.querySelector('.game-screenshots'),
            ...document.querySelectorAll('.sidebar-section')
        ];

        elements.forEach((element, index) => {
            element.style.opacity = '0';
            element.style.transform = 'translateY(20px)';
            element.style.transition = 'opacity 0.5s ease, transform 0.5s ease';
            element.style.transitionDelay = `${index * 0.1}s`;

            requestAnimationFrame(() => {
                element.style.opacity = '1';
                element.style.transform = 'translateY(0)';
            });
        });
    }

    // 初始化页面动画
    initPageAnimations();
});
